const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],

  /**
   * 页面的初始数据
   */
  data: {
    box_no: "",
    fs_upload_url: "https://logisticsengineering.gree.com/container_manage_api/container_manage",
    baseUrl: "https://logisticsengineering.gree.com",
    uploader: [],
    img_src: ""
  },
  scan() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      box_no: "",
      uploader: [],
      img_src: ""
    })

    var _this = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log("图片", res);
        wx.uploadFile({
          url: _this.data.fs_upload_url + '/fs_upload',
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          formData: {
          },
          success(res) {
            const result = JSON.parse(res.data);
            console.log(result)
            if (result.code == 20000) {
              const uploader = _this.data.uploader;

              _this.setData({
                img_src: _this.data.fs_upload_url + '/fs_download?file_id=' + result.data.file_id,
              })

            } else {
              wx.hideLoading();
              wx.showToast({
                title: result.msg,
                icon: "error"
              })
            }

          },
          fail(res) {
            wx.hideLoading()
            wx.showToast({
              title: "未知错误428",
              icon: "error"
            })
          }
        })
        wx.getFileSystemManager().readFile({
          filePath: res.tempFiles[0].tempFilePath, // 图片路径
          encoding: 'base64', // 转换为base64
          success: res => {

            _this.setData({
              base: res.data
            })
            console.log(_this.data.base); // 转换后的base64
            // _this.toBase()

          },
          fail: err => {
            console.log(err);
          }
        })
      },
      fail: err => {
        wx.hideLoading()

        console.log(err);
      }
    })


  },
  toBase() {
    wx.showLoading({
      title: '柜号提取中...',
    })
    let data = {
      img_data: this.data.base
    }
    let url = this.data.baseUrl + "/gm_user/detect_container_id"
    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {
        wx.hideLoading()

        // if (/^[A-Z]{4}\d{7}$/.test(res.data.ContainerId) == false) {
        //   wx.showToast({
        //     title: '识别错误，请重试！',
        //     icon: 'none'
        //   })
        // } else {
        this.setData({
          box_no: res.data.ContainerId
        })
        wx.showToast({
          title: res.msg,
          icon: "success"
        });
        // }

      } else {
        this.setData({
          box_no: "",
          uploader: [],
          img_src: ""
        })
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
    }).catch(err => {
      this.setData({
        box_no: "",
        uploader: [],
        img_src: ""
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  preViewImg(e) {
    console.log(e.target.dataset.url);
    let lists = []
    lists.push(e.target.dataset.url)

    wx.previewImage({
      current: e.target.dataset.url,
      urls: lists,
    })
  },

  onSubmit(event) {
    const that = this
    const url = 'https://logisticsengineering.gree.com/vehicle/print_inspection_pass_ajax3';
    const data = {
      box_no: that.data.box_no
    };
    this.post(url, data).then((res) => {
      if (res.code == 20000) {
        this.setData({
          box_no: "",
          uploader: [],
          img_src: ""
        })
        wx.showToast({
          title: "打印成功",
        })
      }
      console.log("success", res)

    }).catch((err) => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})